package top.happylaishop.shop.utils.resp;

import com.fasterxml.jackson.annotation.JsonIgnore;
import top.happylaishop.shop.common.Const;

public class RestResponse<T> extends BaseResponse {

    public static final RestResponse DEFAULT_NOT_LOGIN = new RestResponse<>(BaseResponse.FAIL.getStatus(), Const.ERROR_MSG_NOTLOGIN);
    public static RestResponse successObjectRestRes = new RestResponse(BaseResponse.OK.getStatus(), BaseResponse.OK.getMessage());
    public static RestResponse failedRestResponse = new RestResponse(BaseResponse.FAIL.getStatus(), BaseResponse.FAIL.getMessage());
    public static RestResponse limitRestResponse = new RestResponse(BaseResponse.LIMIT.getStatus(), BaseResponse.LIMIT.getMessage());

    T data;

    @JsonIgnore
    boolean rel;

    public RestResponse() {}

    public RestResponse(int status, String message) {
        this.setStatus (status);
        this.setMessage (message);
    }

    public static RestResponse defaultSucessData(Object data){
        RestResponse restResponse = new RestResponse();
        restResponse.setData(data);
        restResponse.setStatus (200);
        restResponse.setMessage (BaseResponse.OK.getMessage());
        restResponse.rel=true;
        return restResponse;
    }

    public boolean isRel() {
        return rel;
    }

    public void setRel(boolean rel) {
        this.rel = rel;
    }

    public RestResponse rel(boolean rel) {
        this.setRel(rel);
        return this;
    }

    public RestResponse successData(T data, String msg) {
        this.setData(data);
        this.setStatus (200);
        this.setMessage (msg);
        this.rel=true;
        return this;
    }

    public RestResponse successData(T data) {
        this.setData(data);
        this.setStatus (200);
        this.setMessage (BaseResponse.OK.getMessage());
        this.rel=true;
        return this;
    }

    public static RestResponse failed(String msg) {
        RestResponse response = new RestResponse();
        response.setStatus(500);
        response.rel=false;
        response.setMessage(msg);
        return response;
    }

    public static RestResponse failed() {
        return failed(BaseResponse.failed().getMessage());
    }

    public T getData() {
        return data;
    }

    public RestResponse setData(T data) {
        this.data = data;
        return this;
    }

}
